<template>
  <div class="add_goods">
    <el-form :model="addForm" label-width="100px">
      <el-form-item label="选择商家:">
        <el-select v-model="shopId" @change="changeShopType" filterable placeholder="请选择">
          <el-option
            v-for="item in shopOptions"
            :key="item.id"
            :label="item.shopName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="选择类型:">
        <el-select v-model="addForm.itemType" @change="changeShopType" placeholder="请选择">
          <el-option label="商品" value="1"></el-option>
          <el-option label="优惠券" value="2"></el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="选择商品:">
        <el-select v-model="addForm.itemId" filterable placeholder="请选择">
          <el-option
            v-for="item in goodsOptions"
            :key="item.id"
            :label="item.goodsName ? item.goodsName : item.ticketName"
            :value="item.id">
          </el-option>
        </el-select>
      </el-form-item>

      <el-form-item label="轮播图片:">
        <div class="uploadbtn" @click="uploadBtn">
          <input type="file" @change="upload" multiple ref="file" style="display: none;" />
          <img :src="addForm.fileUrl" v-if="addForm.fileUrl != null">
          <i class="el-icon-upload" v-if="addForm.fileUrl == null" />
        </div>
      </el-form-item>
    </el-form>
    <p style="text-align: center;">
      <el-button size="mini">取消</el-button>
      <el-button size="mini" type="primary" @click="confirm">添加</el-button>
    </p>

  </div>
</template>

<script>
  export default {
    data() {
      return {
        shopOptions: [],
        goodsOptions: [],
        shopId: null,
        addForm: {
          fileUrl: null,
          itemId: null,
          itemType: null,
        }
      }
    },
    mounted() {
      this.getShopInfoList()
    },
    methods: {
      getShopInfoList() {
        this.$axios({
          mehtod: 'get',
          url: '/shop/shop-info/shopInfoList'
        }).then((res) => {
          if(res.code == 200) {
            this.shopOptions = res.data;
          }
        })
      },

      changeShopType() {
        if(this.shopId != null && this.addForm.itemType != null) {
          if(this.addForm.itemType == 1) {
            this.$axios({
              method: 'get',
              url: '/shop/goods/getGoodsByShopId',
              params: {shopId : this.shopId}
            }).then(res => {
              if(res.code == 200) {
                this.goodsOptions = res.data;
              }
            })
          } else {
            this.$axios({
              method: 'post',
              url: '/shop/shop-ticket/getTicketByShopId',
              data: this.$qs.stringify({shopId: this.shopId})
            }).then(res => {
              if(res.code == 200) {
                this.goodsOptions = res.data;
              }
            })
          }
        }
      },

      confirm() {
        this.$axios({
          method: 'post',
          url: '/shop/shop-slide-show/add',
          data: this.$qs.stringify(this.addForm)
        }).then(res => {
          if(res.code == 200) {
            this.$message.success('添加商品成功');
            this.$emit('closeDialog',false);
            this.$emit('refresh');
          }
        })
      },


      uploadBtn(){
        this.$refs.file.dispatchEvent(new MouseEvent('click'))
      },

      upload(e) {
        let files = e.target.files
        this.$nextTick(() => {
            // this.option.img = URL.createObjectURL(files[0]);
            // this.fileinfo = files[0].name;
            // this.dialogVisible = true
        })
        for (let i = 0; i < files.length; i++) {
          const isImg = files[i].type.indexOf('image') > -1
          const isLimit = files[i].size / 1024 / 1024 < 10

          if (!isImg) {
            this.$message.error('请上传图片格式!')
            return false
          }
          if (!isLimit) {
            this.$message.error('大小不能超过 10MB!')
            return false
          }
          let formData = new FormData()
          formData.append('files', files[i])

          var image = { url: null, percent: 0 }
          // this.imageList.push(image)

          this.uploadAxios(formData, image)
        }
      },

      uploadAxios(formData, image){
        this.$axios({
          url: 'https://file.zmzncs.com/upload/upload-batch',
          method: 'post',
          data: formData,
          timeout: 1000 * 60, // 请求超时时间
          onUploadProgress: (progress)=> {
            image.percent = Math.round(progress.loaded / progress.total * 100)
          }
        }).then((res) => {
          if (res.code == 200) {
            this.addForm.fileUrl = res.data[0].url;
            image.percent = 100
            image.url = res.data[0].url
            // this.imgs.push(image.url)
            //  组件返回值
            // var urlList = []
            // this.imgs = [];
            // this.imageList.forEach((image)=> {
            //   this.imgs.push(image.url)
            // })
            // var urls = urlList.toString()
            // this.$emit('input', urls)
          }
        }).catch(err => {})
      },
    }
  }
</script>

<style scoped>
  .uploadbtn {
    border: 1px solid #aaa;
    height: 100px;
    width: 100px;
    position: relative;
  }

  .uploadbtn img {
    max-width: 100%;
    max-height: 100%;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

  .el-icon-upload {
    font-size: 50px;
    position: absolute;
    top: 50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }


</style>
